<template>
  <div>
    <div class="bigbox">
      <div class="box" v-for="item in books" :key="item._id">
        <img
          class="img"
          :src="`http://statics.zhuishushenqi.com`+`${item.cover}`"
          alt
          @click="jump(item._id)"
        />
        <div class="right">
          <p class="p1">{{item.title}}</p>
          <p class="p2">
            <span class="s1" @click="jumpSearch(item.author)">{{item.author}}</span>
            <span>&nbsp;|&nbsp;</span>
            <span class="s2" @click="jumpSearch(item.majorCate)">{{item.majorCate}}</span>
          </p>
          <p class="s3">{{item.shortIntro}}</p>
          <p class="p3">
            <span class="s1">
              人气:
              <span class="x2">{{item.latelyFollower}}</span>
            </span>
            <span>&nbsp;|&nbsp;</span>
            <span class="s2">
              读者留存:
              <span class="x1">{{item.retentionRatio}}%</span>
            </span>
          </p>
        </div>
      </div>
    </div>
    <!-- 分页器 -->
    <div class="pagination">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        layout=" prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { sortBook } from "@/api/cjs/sort";
export default {
  data() {
    return {
      books: [],
      cover: "",
      total: 0,
      oldBook: []
    };
  },
  props: ["go"],
  created() {
    // setTimeout(() => {
    sortBook(this.go).then(res => {
      // this.books = res.data.ranking.books;

      // this.books.forEach(function(item) {
      //   let url = item.cover.slice(7);
      //   let urlOk = decodeURIComponent(url);
      //   item.cover = urlOk;
      // });
      this.books = res.data.ranking.books.slice(0, 10);

      this.oldBook = res.data.ranking.books;

      this.total = res.data.ranking.books.length;
    });
    // }, 40);
  },
  watch: {
    go(newVal) {
      // setTimeout(() => {
      sortBook(this.go).then(res => {
        // this.books = res.data.ranking.books;

        // this.books.forEach(function(item) {
        //   let url = item.cover.slice(7);
        //   let urlOk = decodeURIComponent(url);
        //   item.cover = urlOk;
        // });
        this.books = res.data.ranking.books.slice(0, 10);

        this.oldBook = res.data.ranking.books;

        this.total = res.data.ranking.books.length;
      });
      // }, 40);
    }
  },
  methods: {
    jump(id) {
      localStorage.setItem("_id", id);
      this.$router.push("/bookdetails");
    },
    handleCurrentChange(pageNum) {
      let start = (pageNum - 1) * 10;
      let end = pageNum * 10;
      this.books = this.oldBook.slice(start, end);
    },
    jumpSearch(str) {
      this.$router.push({ path: "/search", query: { id: str } });
    }
  }
};
</script>

<style scoped>
.bigbox,
.box {
  display: flex;
  justify-content: space-between;
  text-align: left;
}
.bigbox {
  width: 900px;
  flex-wrap: wrap;
}
.box {
  margin: 20px 20px;
}
.img {
  width: 80px;
  height: 100px;
  cursor: pointer;
}
.right {
  width: 300px;
  height: 100px;
  display: block;
  padding-left: 25px;
}
p {
  display: block;
}
.p1 {
  font-size: 20px;
  line-height: 24px;
  cursor: pointer;

  font-weight: 600;
}
.p2 {
  line-height: 28px;
  color: #a1a5a1;
}
.s3 {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 12px;
  margin-top: 10px;
}
.s1,
.s2 {
  font-size: 12px;
  cursor: pointer;
}

.x2 {
  color: red;
}
.x1 {
  color: red;
}
</style>
